/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.bme.j2ee.v37zen.beans;

import hu.bme.j2ee.v37zen.entities.User;
import hu.bme.j2ee.v37zen.util.QueryException;
import hu.bme.j2ee.v37zen.util.Role;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Kiss Dániel
 */
@Stateless
@LocalBean
public class FindUsersBean {

    @PersistenceContext
    EntityManager em;
    
    @SuppressWarnings("unchecked")
    public boolean isUserTableEmpty(){
        Query query = em.createNamedQuery("User.findAll");
        return query.setMaxResults(1).getResultList().isEmpty();
    }
    
    @SuppressWarnings("unchecked")
    public List<User> findAllUser(){
        Query query = em.createNamedQuery("User.findAll");
        return query.getResultList();
    }
    
    @SuppressWarnings("unchecked")
    public List<User> findUserByRoleSet(EnumSet<Role> roles){
        if(roles == null || roles.isEmpty()){
            return new ArrayList<User>();
        }
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<User> query = builder.createQuery(User.class);
        
        Root<User> user = query.from(User.class);
        
        query.select(user).where(user.get("roles").in(roles));
        query.distinct(true);
        
        return em.createQuery(query).getResultList();
    }
    
    @SuppressWarnings("unchecked")
    public User findUserByUsername(String username){
        try{
            return this.findUserByUsernameSafe(username);
        } catch (QueryException qe){
            Query query = em.createNamedQuery("User.findUserByUsername");
            query.setParameter("username", username);
            List<User> list = query.getResultList();
            if(list.isEmpty()){
                return null;
            }
            return list.get(0);
        }
    }

    @SuppressWarnings("unchecked")
    public User findUserByUsernameSafe(String username) throws QueryException{
        Query query = em.createNamedQuery("User.findUserByUsername");
        query.setParameter("username", username);
        List<User> list = query.getResultList();
        if(list.size() > 1){
            throw new QueryException("Multipe user with identical username found!");
        }
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
    
    
    @SuppressWarnings("unchecked")
    public User findUserByUsernameEager(String username){
        try{
            return this.findUserByUsernameSafe(username);
        } catch (QueryException qe){
            Query query = em.createNamedQuery("User.findUserByUsername");
            query.setParameter("username", username);
            List<User> list = query.getResultList();
            if(list.isEmpty()){
                return null;
            }
            return list.get(0);
        }
    }
    
    @SuppressWarnings("unchecked")
    public User findUserByUsernameEagerSafe(String username) throws QueryException{
        Query query = em.createNamedQuery("User.findUserByUsernameEager");
        query.setParameter("username", username);
        List<User> list = query.getResultList();
        if(list.size() > 1){
            throw new QueryException("Multipe user with identical username found!");
        }
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
    
    @SuppressWarnings("unchecked")
    public User findUserByDialID(long id) {
        Query query = em.createNamedQuery("User.findUserByDialID");
        query.setParameter("id", id);
        List<User> list = query.getResultList();
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
    
    public boolean isUsernameAvailable(String username){
        try{
            if(this.findUserByUsernameSafe(username) != null){
                return false;
            }
        } catch (QueryException qe){
            return false;
        }
        return true;        
    }
    
    @SuppressWarnings("unchecked")
    public List<User> findUsersByLoginState(boolean logedIn){
        Query query = em.createNamedQuery("User.findUserByLoginState");
        query.setParameter(":logedIn", logedIn);
        return query.getResultList();
    }
    
    @SuppressWarnings("unchecked")
    public User findUserByEmail(String email){
        try{
            return this.findUserByEmailSafe(email);
        } catch (QueryException qe){
            Query query = em.createNamedQuery("User.findUserByEmail");
            query.setParameter("email", email);
            List<User> list = query.getResultList();
            if(list.isEmpty()){
                return null;
            }
            return list.get(0);
        }
    }
    
    @SuppressWarnings("unchecked")
    public User findUserByEmailSafe(String email) throws QueryException{
        Query query = em.createNamedQuery("User.findUserByEmail");
        query.setParameter("email", email);
        List<User> list = query.getResultList();
        if(list.size() > 1){
            throw new QueryException("Multipe user with identical email found!");
        }
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
    
    public boolean isEmailAvailable(String email){
        try{
            if(this.findUserByUsernameSafe(email) != null){
                return false;
            }
        } catch (QueryException qe){
            return false;
        }
        return true;        
    }
}
